home *** CD-ROM | disk | FTP | other *** search
/ Multimedia Jumpstart / Multimedia Microsoft Jumpstart Version 1.1a (Microsoft).BIN / develpmt / source / rleapp / mem.asm < prev    next >
Encoding:
Assembly Source File  |  1992-09-12  |  6.4 KB  |  289 lines

  1.         TITLE MEM.ASM
  2.  
  3. ;****************************************************************
  4. ;* MEM.ASM - Assembly mem-fill and mem-copy routines        *
  5. ;****************************************************************
  6.  
  7. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  8. ;
  9. ; (C) Copyright Microsoft Corp. 1991.  All rights reserved.
  10. ;
  11. ; You have a royalty-free right to use, modify, reproduce and 
  12. ; distribute the Sample Files (and/or any modified version) in 
  13. ; any way you find useful, provided that you agree that 
  14. ; Microsoft has no warranty obligations or liability for any 
  15. ; Sample Application Files which are modified. 
  16. ;
  17. ; Unless you got this from the MM Sys BBS, it may not be the most
  18. ; current version.  We are continually improving our samples and
  19. ; their documentation.  Call the BBS at 206 936-4082.
  20. ;
  21. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  22.  
  23.  
  24. ?PLM=1        ; PASCAL Calling convention is DEFAULT
  25. ?WIN=1        ; Windows calling convention
  26. ?386=0        ; Use 386 code?
  27.  
  28. .xlist
  29. include cmacros.inc
  30. include windows.inc
  31. .list
  32.  
  33.     externA        __WinFlags        ; in KERNEL
  34.     externA        __AHINCR        ; in KERNEL
  35.     externA        __AHSHIFT        ; in KERNEL
  36.  
  37. ; The following structure should be used to access high and low
  38. ; words of a DWORD.  This means that "word ptr foo[2]" -> "foo.hi".
  39.  
  40. LONG    struc
  41. lo    dw    ?
  42. hi    dw    ?
  43. LONG    ends
  44.  
  45. FARPOINTER    struc
  46. off    dw    ?
  47. sel    dw    ?
  48. FARPOINTER      ends
  49.  
  50. EAXtoDXAX   macro
  51.         shld    edx,eax,16      ; move HIWORD(eax) to dx
  52.         endm
  53.  
  54. DXAXtoEAX   macro
  55.         ror     eax,16          ; xchg HIWORD(eax) and LOWORD(eax)
  56.         shrd    eax,edx,16      ; move LOWORD(edx) to HIWORD(eax)
  57.         endm
  58.  
  59. ; -------------------------------------------------------
  60. ;        DATA SEGMENT DECLARATIONS
  61. ; -------------------------------------------------------
  62.  
  63. ifndef SEGNAME
  64.     SEGNAME equ <_TEXT>
  65. endif
  66.  
  67. createSeg %SEGNAME, CodeSeg, word, public, CODE
  68.  
  69. sBegin Data
  70. sEnd Data
  71.  
  72. sBegin CodeSeg
  73. assumes cs,CodeSeg
  74. assumes ds,DATA
  75.  
  76. ;---------------------------Public-Routine------------------------------;
  77. ; muldiv32
  78. ;
  79. ; multiples two 32 bit values and then divides the result by a third
  80. ; 32 bit value with full 64 bit presision
  81. ;
  82. ; lResult = (lNumber * lNumerator) / lDenominator
  83. ;
  84. ; Entry:
  85. ;       lNumber = number to multiply by nNumerator
  86. ;       lNumerator = number to multiply by nNumber
  87. ;       lDenominator = number to divide the multiplication result by.
  88. ;   
  89. ; Returns:
  90. ;       DX:AX = result of multiplication and division.
  91. ; Error Returns:
  92. ;       none
  93. ; Registers Preserved:
  94. ;       DS,ES,SI,DI
  95. ;-----------------------------------------------------------------------;
  96.         assumes ds,nothing
  97.         assumes es,nothing
  98.  
  99. cProc   muldiv32,<PUBLIC,FAR>,<>
  100.         ParmD  ulNumber
  101.         ParmD  ulNumerator
  102.         ParmD  ulDenominator
  103. cBegin
  104.         .386
  105.         mov     eax,ulNumber
  106.         mov     edx,ulNumerator
  107.         mov     ebx,ulDenominator
  108.  
  109.         imul    edx     ; edx:eax = (ulNumber * ulNumerator)
  110.         idiv    ebx     ; eax     = (ulNumber * ulNumerator) / ulDenominator
  111.  
  112.         EAXtoDXAX       ; covert eax to dx:ax for 16 bit programs
  113.         .286
  114. cEnd
  115.  
  116. ;---------------------------Public-Routine------------------------------;
  117. ; MemFill
  118. ;
  119. ;   fills memory with a bunch of bytes
  120. ;
  121. ; Entry:
  122. ;    lpMem    LPSTR to memory to fill
  123. ;    cbMem    DWORD count of bytes to fill
  124. ;    bFill    BYTE  byte to fill
  125. ;
  126. ; Returns:
  127. ;    nothing
  128. ; Error Returns:
  129. ;    None
  130. ; Registers Preserved:
  131. ;    BP,DS,SI,DI
  132. ; Registers Destroyed:
  133. ;    AX,BX,CX,DX,FLAGS
  134. ; Calls:
  135. ;    nothing
  136. ; History:
  137. ;
  138. ;    Wed 04-Jan-1990 13:45:58 -by-  Todd Laney [ToddLa]
  139. ;    Created.
  140. ;-----------------------------------------------------------------------;
  141.  
  142. cProc MemFill,<FAR,PUBLIC,NODATA>,<>
  143.         ParmD   lpMem
  144.         ParmD   cbMem
  145.         ParmB   bFill
  146. cBegin
  147.     .386
  148.     push    edi
  149.  
  150.     cld
  151.     mov    bl, byte ptr bFill        ; repeat the byte through EAX
  152.     mov    bh, bl
  153.     mov    ax,bx
  154.     shl    eax,16
  155.     mov    ax,bx
  156.  
  157.     les    di, lpMem
  158.     movzx    edi,di
  159.     mov    ebx,cbMem
  160.  
  161.     mov    ecx,edi
  162.     neg    ecx
  163.     and    ecx,0011b
  164.     sub    ebx,ecx
  165.     rep    stos byte ptr es:[edi]        ; note can optimize WORD/DWORD writes
  166.     db    67H        ; Fix strange 386 bug
  167.     mov    ecx,ebx
  168.     shr    ecx,2
  169.     rep    stos dword ptr es:[edi]
  170.     db    67H        ; Fix strange 386 bug
  171.     mov    ecx,ebx
  172.     and    ecx,0011b
  173.     rep    stos byte ptr es:[edi]
  174.     db    67H        ; Fix strange 386 bug
  175.  
  176. mf386_exit:
  177.     pop    edi
  178.     .286
  179. cEnd
  180.  
  181. ;---------------------------Public-Routine------------------------------;
  182. ; MemCopy
  183. ;
  184. ;   copy memory
  185. ;
  186. ; Entry:
  187. ;    lpSrc    HPSTR to copy from
  188. ;    lpDst    HPSTR to copy to
  189. ;    cbMem    DWORD count of bytes to move
  190. ;
  191. ;       NOTE: overlapped copies will work
  192. ;
  193. ; Returns:
  194. ;    destination pointer
  195. ; Error Returns:
  196. ;    None
  197. ; Registers Preserved:
  198. ;    BP,DS,SI,DI
  199. ; Registers Destroyed:
  200. ;    AX,BX,CX,DX,FLAGS
  201. ; Calls:
  202. ;    nothing
  203. ; History:
  204. ;-----------------------------------------------------------------------;
  205.  
  206. cProc MemCopy,<FAR,PASCAL,PUBLIC,NODATA>,<ds>
  207.     ParmD    lpDst
  208.     ParmD    lpSrc
  209.     ParmD    cbMem
  210. cBegin
  211.     .386
  212.     push    edi
  213.     push    esi
  214.     cld
  215.  
  216.     mov    ecx,cbMem
  217.     jecxz    mc386_exit
  218.  
  219.     movzx    edi,di
  220.     movzx    esi,si
  221.     lds    si,lpSrc
  222.     les    di,lpDst
  223. ;
  224. ; calculate differance of pointers in "selector" space
  225. ;
  226.     mov    ax,si        ; DX:AX = lpSrc
  227.     mov    dx,ds
  228.  
  229.     mov    bx,es        ; BX = selector of ptr B
  230.  
  231.     mov    cx,__AHSHIFT    ; number of selector bits per 64K 'segment'
  232.     shr    dx,cl        ; linearize ptr A
  233.     shr    bx,cl        ; linearize ptr B
  234. ;
  235. ; DX and BX contain normalized selectors
  236. ;
  237.         mov     ecx,cbMem
  238.  
  239.         sub     ax,di
  240.         sbb     dx,bx              ; do long subtraction.
  241.         jnc     mc_copy_forward
  242.  
  243.     add    ax,cx
  244.     adc    dx,cbMem.hi
  245.         jnc     mc_copy_forward    ; carry, so >0, thus they do hit.
  246.  
  247.     std
  248.     add    edi,ecx
  249.     add    esi,ecx
  250.  
  251.     sub    edi,4
  252.     sub    esi,4
  253.  
  254.     push    ecx
  255.     shr    ecx,2        ; get count in DWORDs
  256.     rep    movs dword ptr es:[edi], dword ptr ds:[esi]
  257.     db    67H        ; Fix strange 386 bug
  258.     add    edi,3
  259.     add    esi,3
  260.     pop    ecx
  261.     and    ecx,3
  262.     rep    movs byte ptr es:[edi], byte ptr ds:[esi]
  263.     db    67H        ; Fix strange 386 bug
  264.     jmp    mc386_exit
  265.  
  266. mc_copy_forward:
  267.     push    ecx
  268.     shr    ecx,2        ; get count in DWORDs
  269.     rep    movs dword ptr es:[edi], dword ptr ds:[esi]
  270.     db    67H
  271.     pop    ecx
  272.     and    ecx,3
  273.     rep    movs byte ptr es:[edi], byte ptr ds:[esi]
  274.     db    67H
  275.     nop
  276. mc386_exit:
  277.     cld
  278.     pop    esi
  279.     pop    edi
  280.     mov    dx,lpDst.sel    ; return destination address
  281.     mov    ax,lpDst.off
  282.     .286
  283. cEnd
  284.  
  285. sEnd
  286.  
  287. sEnd CodeSeg
  288. end
  289.